﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FileDelete
{
    public static class FileDeleteTools
    {
        /// <summary>
        /// 删除指定日期格式名称的文件夹
        /// </summary>
        /// <param name="srcPath"></param>
        /// <param name="dateString"></param>
        /// <returns></returns>
        public static bool FileDelete(string srcPath,string dateString = null)
        {
            try
            {
                DirectoryInfo dir = new DirectoryInfo(srcPath);
                FileSystemInfo[] fileinfo = dir.GetFileSystemInfos();  //返回目录中所有文件和子目录
                foreach (FileSystemInfo i in fileinfo)
                {
                    if (i is DirectoryInfo)            //判断是否文件夹
                    {
                        if (i.Name == dateString)
                        {
                            DirectoryInfo subdir = new DirectoryInfo(i.FullName);
                            subdir.Delete(true);          //删除子目录和文件
                        }                       
                    }
                    //else
                    //{
                    //    File.Delete(i.FullName);      //删除指定文件
                    //}
                }
                return true;
            }
            catch (Exception ex)
            {
                throw;
            }
        }

        //public static bool FileDelete(string srcPath, string dateString = null)
        //{
        //    try
        //    {
        //        DirectoryInfo dir = new DirectoryInfo(srcPath);
        //        FileSystemInfo[] fileinfo = dir.GetFileSystemInfos();  //返回目录中所有文件和子目录
        //        foreach (FileSystemInfo i in fileinfo)
        //        {
        //            if (i is DirectoryInfo)            //判断是否文件夹
        //            {
        //                DirectoryInfo subdir = new DirectoryInfo(i.FullName);
        //                subdir.Delete(true);          //删除子目录和文件
        //            }
        //            else
        //            {
        //                File.Delete(i.FullName);      //删除指定文件
        //            }
        //        }
        //        return true;
        //    }
        //    catch (Exception ex)
        //    {
        //        throw;
        //    }
        //}


        /// <summary>
        /// 弹出一个选择目录的对话框
        /// </summary>
        /// <returns></returns>
        public static string SelectPath()
        {
            FolderBrowserDialog path = new FolderBrowserDialog();
            path.ShowDialog();
            return path.SelectedPath;
        }

        /// <summary>
        /// 弹出一个选择文件的对话框
        /// </summary>
        /// <returns></returns>
        public static string SelectFile()
        {
            OpenFileDialog file = new OpenFileDialog();
            file.ShowDialog();
            return file.SafeFileName;
        }

    }
}
